package qeug.shsh.facde.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.drew.lang.annotations.NotNull;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.openapi.common.custom.annotation.*;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.epm.eb.service.BgControlServiceImpl;
import qeug.shsh.facde.info.BPMApplyModel;
import qeug.shsh.facde.info.BgModel;
import qeug.shsh.facde.info.BgResult;
import qeug.shsh.facde.resultext.UserResultExt;

import javax.validation.Valid;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@ApiController(value = "bos", desc = "预算控制")
@ApiMapping(value = "BgControl")
public class BgControlController implements Serializable {
    @ApiPostMapping(value = "query")
    public CustomApiResult<@ApiResponseBody("返回参数") BgResult> query(@NotNull @Valid @ApiRequestBody(value = "入参") BgModel model) {


//        BgResult bgresult = new BgResult();
        BgControlServiceImpl bgControlService = new BgControlServiceImpl();
        ArrayList<Map<String, String>> members = makeReslutMap(model);
        Long modelId = model.getModelId();
        String type = model.getType();
        HashSet<String> typeSet = new HashSet();
        typeSet.add(type);
        String json = bgControlService.queryBalanceWithoutCentralized(modelId, members, typeSet, false);
        JSONArray array = JSONObject.parseArray(json);

        JSONObject jsonObject = array.getJSONObject(0);
        BgResult bgresult = JSONObject.toJavaObject(jsonObject, BgResult.class);
        CustomApiResult<BgResult> result = CustomApiResult.success(bgresult);

        return result;
    }


    public ArrayList<Map<String,String>> makeReslutMap( BgModel model){
        ArrayList<Map<String,String>> resultMap = new ArrayList<Map<String, String>>();
        Map<String, String> map = new HashMap<String, String>();
        //成本中心
        map.put("Entity", model.getEntity());

        //预算项目
        map.put("Account", model.getAccount());

        //版本
        map.put("Version", model.getVersion());

        //币种
        map.put("Currency", model.getCurrency());

        //线索
        map.put("AuditTrail", model.getAuditTrail());

        //变动类型
        map.put("ChangeType", model.getChangeType());

        //数据类型
        map.put("DataType", model.getDataType());

        //度量
        map.put("Metric", model.getMetric());
        map.put("BudgetPeriod", model.getBudgetPeriod());



        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DynamicObject temp = new DynamicObject();
//        if("lastmonth".equals(period)){
//            // 获取上个月的1号
//            LocalDate usageDay = today.minusMonths(1).withDayOfMonth(1);
//            // 将 LocalDate 转换为 date
//            Date date = Date.from(usageDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
//            QFilter shownumber = new QFilter("startdate", "=", date).and(new QFilter("isleaf", QCP.equals, "1"));
//            temp = BusinessDataServiceHelper.loadSingle("epm_bperiodmembertree","shownumber",  shownumber.toArray());
//        }else if("thismonth".equals(period)){
//            // 获取本月的1号
//            LocalDate usageDay = today.withDayOfMonth(1);
//            // 将 LocalDate 转换为 date
//            Date date = Date.from(usageDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
//            QFilter shownumber = new QFilter("startdate", "=", date).and(new QFilter("isleaf", "=", "1"));
//            temp = BusinessDataServiceHelper.loadSingle("epm_bperiodmembertree","shownumber",  shownumber.toArray());
//        }
        resultMap.add(map);
        return resultMap;
    }
}
